<?php

namespace App\Http\Controllers\Admin\Anchor;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Anchor\AnchorTypeRepository;
use Illuminate\Http\Request;

// 主播
class TypeController extends BaseController
{
    private $repoist;

    public function __construct(AnchorTypeRepository $repository)
    {
        $this->repoist = $repository;
    }

    public function add(Request $request)
    {
        return $this->repoist->add($request);
    }

    public function index(Request $request)
    {
        $where = [];
        if ($request->has('name') && !empty($request->get('name'))) {
            $where['where']['name'] = $request->get('name');
        }
        if (($request->has('status')) && ($request->get('status') != '') && ($request->get('status') != -1)) {
            $where['where']['status'] = (int)$request->get('status');
        }
//        print_r($request);
        if ($request->has('dateRange') && !empty($request->get('dateRange'))) {
            $time = $request->get('dateRange');
            $where['between'] = ['created_at' => [$time]];
        }
        $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
        $where['limit']['number'] = $request->get('pageSize');
//         var_dump($where);
        $data = $this->repoist->index($where);
        return $data;
    }

    public function edit(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }

        return $this->repoist->edit($request);
    }

    public function del(Request $request, $id)
    {
        if (!$id) {
            return $this->returnMsg(1, '请传递参数');
        }
        return $this->repoist->del($id);
    }

    public function changeStatus(Request $request)
    {
        $id = $request->get("id");
        // 需要修改两个地方 一个是状态 一个 app是否上架
        $status = (int)$request->get('status');
        return $this->repoist->updateStatus($id, $status);
    }

//    返回类型给前端做选择
    public function typeList(Request $request)
    {
        $data = $this->repoist->unifiedGetAll(['status' => 0], ['id', 'name']);
        return $this->returnMsg(200, '数据已获取', $data);
    }
}
